<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>set-操作数组</title>
</head>
<body>
<script>
    let arr = [1, 2, 3, 3, 4, 5, 4, 6, 5, 7, 2, 8, 3, 1]

    // 1. 数组去重 将数组转换为集合就自动去重
    // let result= [...new Set(arr)]//去重后将集合转换为数组
    // console.log(result)

    // 2. 交集
    // let arr1 = [3, 2, 5, 4, 7, 0, 9]
    // let result=[...new Set(arr)].filter(item=>{
    //     let result1=new Set(arr1)
    //     if(result1.has(item)){
    //         return true;
    //     }
    //     else {
    //         return false;
    //     }
    // })
    // console.log(result)
    //简写
    // let result=[...new Set(arr)].filter(item=>new Set(arr1).has(item))


    //并集
    // 方法一
    // let arr1 = [3, 2, 5, 4, 7, 0, 9]
    // let new_arr=arr.concat(arr1)
    // let result=[...new Set(new_arr)]
    // console.log(result)
    // * 方法二
    // let arr1 = [3, 2, 5, 4, 7, 0, 9]
    // let result=[...new Set([...arr,...arr1])]
    // console.log(result)


    //差集
    // let arr1 = [3, 2, 5, 4, 7, 0, 9]
    // let result=[...new Set(arr)].filter(item=>{
    //     let result1=new Set(arr1)
    //     if(result1.has(item)){
    //         return false;
    //     }
    //     else {
    //         return true;
    //     }
    // })
    // console.log(result)

    //补集
    let arr1 = [3, 2, 5, 4, 7, 0, 9]
    let result1=[...new Set(arr)].filter(item=>!new Set(arr1).has(item))
    let result2=[...new Set(arr1)].filter(item=>!new Set(arr).has(item))
    let result=[...new Set([...result1,...result2])]
    console.log(result)
</script>
</body>
</html>